package com.mall.product.controller;

import com.mall.common.response.CommonResponse;
import com.mall.constant.enums.CommonStatus;
import com.mall.entity.TbType;
import com.mall.product.service.TypeService;
import com.mall.product.vo.VoCcascade;
import com.mall.product.vo.VoType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 型控制器
 *
 * @author ASUS
 * @date 2021/07/18
 */
@RestController
@RequestMapping("/type")
@CrossOrigin
public class TypeController {
    @Autowired
    private TypeService typeService;

    /**
     * 找到列表
     *
     * @param id id
     * @return {@link List<TbType>}
     */
    @GetMapping("/findList")
    public List<TbType> findList(@RequestParam("id")Integer id){
        return typeService.findListByPid(id);
    }


    /**
     * @date 2021/7/18 20:05
     * @version 1.0
     * 商品分类列表
     */
    @GetMapping("/findAll")
    public List<TbType> findAll(){
        return typeService.findAll();
    }

    /**
     * 查询商品级别分类
     * @param tbType
     * @return
     */
    @PostMapping("/findTypeById")
    public List<VoType> findTypeById(@RequestBody TbType tbType){

        return typeService.findTypeById(tbType);
    }

    /**
     * 根据id修改状态
     * @param id
     * @param status
     */
    @PutMapping("/upStatus")
    public void upStatus(Integer id,Integer status){
        typeService.upStatus(id,status);
    }


    /**
     * 查询父级的pid
     * @param pid
     * @return
     */
    @GetMapping("/findTypePid")
    public List<VoType> findTypePid(Integer pid){
        return typeService.findTypePid(pid);
    }

    /**
     * 商品根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findOne")
    public TbType findOne(Integer id){
        return typeService.findOne(id);
    }

    /**
     * 添加商品分类
     * @param tbType
     */
    @PostMapping("/addType")
    public CommonResponse addType(@RequestBody TbType tbType){
        try {
            typeService.addType(tbType);
            return new CommonResponse(CommonStatus.VALID.getCode(),"添加成功" );
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.UNVALID.getCode(), "添加失败" );
        }

    }

    /**
     * 添加父类
     * @param tbType
     */
    @PostMapping("/add")
    public CommonResponse add(@RequestBody TbType tbType){
        try {
            typeService.add(tbType);
            return new CommonResponse(CommonStatus.VALID.getCode(),"添加成功" );
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.UNVALID.getCode(), "添加失败" );
        }

    }

    /**
     * 修改商品分类
     * @param tbType
     */
    @PostMapping("/updateType")
    public CommonResponse updateType(@RequestBody TbType tbType){
        try {
            typeService.updateType(tbType);
            return new CommonResponse(CommonStatus.VALID.getCode(),"修改成功" );
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.UNVALID.getCode(), "修改失败" );
        }

    }

    /**
     * 商品分类删除
     * @param id
     */
    @DeleteMapping("/delete")
    public CommonResponse delete(Integer id){
        try {
            typeService.delete(id);
            return new CommonResponse(CommonStatus.VALID.getCode(),"删除成功" );
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.UNVALID.getCode(), "删除失败" );
        }

    }


    /**
     * 模糊查询
     * @param type
     * @return
     */
    @GetMapping("/findShopType")
    public List<VoType> findShopType(@RequestBody TbType type){
        return typeService.findShopType(type);
    }

    /**
     * 级联
     * @param pid
     * @return
     */
    @GetMapping("/findId")
    public List<VoCcascade> findId(Integer pid){
        return typeService.findId(pid);
    }

}
